<?php

declare(strict_types=1);
/**
 * This file is part of MoChat.
 * @link     https://mo.chat
 * @document https://mochat.wiki
 * @contact  group@mo.chat
 * @license  https://github.com/mochat-cloud/mochat/blob/master/LICENSE
 */
namespace MoChat\Plugin\TenantOrder\Action\Admin;

use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\Middleware;
use Hyperf\HttpServer\Annotation\Middlewares;
use Hyperf\HttpServer\Annotation\RequestMapping;
use MoChat\App\Tenant\Contract\TenantContract;
use MoChat\Framework\Action\AbstractAction;
use MoChat\Framework\Request\ValidateSceneTrait;
use MoChat\Plugin\AdminUser\Contract\AdminUserContract;
use MoChat\Plugin\AdminUser\Middleware\AdminAuthMiddleware;
use MoChat\Plugin\AdminUser\Middleware\PermissionMiddleware;
use MoChat\Plugin\TenantOrder\Contract\TenantOrderContract;
use MoChat\Plugin\TenantProduct\Contract\TenantProductSkuContract;

/**
 * 财务管理- 订单列表.
 *
 * Class Index.
 * @Controller
 */
class Index extends AbstractAction
{
    use ValidateSceneTrait;

    /**
     * @Inject
     * @var TenantOrderContract
     */
    protected $tenantOrderService;

    /**
     * @Inject
     * @var TenantContract
     */
    protected $tenantService;

    /**
     * @Inject
     * @var AdminUserContract
     */
    protected $adminUserService;

    /**
     * @Inject
     * @var TenantProductSkuContract
     */
    protected $tenantProductSkuService;

    /**
     * @RequestMapping(path="/admin/tenantOrder/index", methods="get")
     * @Middlewares({
     *     @Middleware(AdminAuthMiddleware::class),
     *     @Middleware(PermissionMiddleware::class)
     * })
     * @return array 返回数组
     */
    public function handle(): array
    {
        // 获取当前登录用户
        $adminUser = user();
        // 参数验证
        $this->validated($this->request->all());

        // 接收参数
        $params = [
            'phone' => trim($this->request->input('phone', '')),
            'status' => (int) $this->request->input('status', 0),
            'startTime' => $this->request->input('startTime', ''),
            'endTime' => $this->request->input('endTime', ''),
        ];

        $creatorId = 0;
        // 代理登陆
        if ($adminUser['isRoot'] === 0) {
            $creatorId = $adminUser['id'];
        }
        $status = [];
        if ($params['status'] === 1) {
            $status = ['fulfilled'];
        }// 成功
        if ($params['status'] === 2) {
            $status = ['checkout', 'new'];
        }// 等待中
        if ($params['status'] === 3) {
            $status = ['cancelled'];
        }// 失败

        $list['data'] = $this->tenantOrderService->getTenantOrderListBySearch($creatorId, $params['phone'], $status, $params['startTime'], $params['endTime']);
        foreach ($list['data'] as &$item) {
            $item['total'] = $item['total'] / 100;
            $item['tenantName'] = $this->tenantService->getTenantByIdWithTrashed($item['tenantId'], ['name'])['name'];
            $item['userName'] = $this->adminUserService->getAdminUserByIdWithTrashed($item['userId'], ['name'])['name'];
            $sku = $this->tenantProductSkuService->getTenantProductSkuByIdWithTrashed($item['skuId'], ['attributes']);
            $attributes = $this->tenantProductSkuService->getTenantProductSkuValuesById(explode(',', $sku['attributes']));
            foreach ($attributes as $v) {
                if ($v['attributeName'] === '套餐名称') {
                    $item['productName'] = $v['attributeValue'];
                    break;
                }
            }
        }
        return $list;
    }

    /**
     * 验证规则.
     *
     * @return array 响应数据
     */
    protected function rules(): array
    {
        return [
        ];
    }

    /**
     * 验证错误提示.
     * @return array 响应数据
     */
    protected function messages(): array
    {
        return [
        ];
    }
}
